#include <boost/asio/ip/tcp.hpp>
#include <cstdint>
#include <iostream>
#include <unistd.h>
#include "MessageBuffer.hpp"
#include "NetworkThread.hpp"
#include "Socket.hpp"
#include "Server.hpp"

class WorldSocket : public Socket<WorldSocket>
{
public:
  WorldSocket(boost::asio::ip::tcp::socket&& sc) : Socket{std::move(sc)}{}
public:
  bool HandleRead() override
  {
    MessageBuffer& msg = GetReadBuffer();
    if(msg.GetActiveSize() > 0)
    {
      std::string str((const char*)msg.GetReadPointer(), msg.GetActiveSize());
      std::cout << "revice msg: " << str << std::endl;
    }
    msg.ReadCompleted(msg.GetActiveSize());
    return true;
  }
};

class WorldNetworkThread : public NetworkThread<WorldSocket>
{
public:
  void AddSocketed(std::shared_ptr<WorldSocket> new_sc) override
  {
    std::cout << "add socket, ip:" << new_sc->GetRemoteAddress() << std::endl;
  }

  void RemoveSocketed(std::shared_ptr<WorldSocket> rm_sc) override
  {
    std::cout << "remove socket, ip:" << rm_sc->GetRemoteAddress() << std::endl;
  }
};

int main(int argc, char* argv[]) 
{
  Server<WorldNetworkThread> svr(7001, 1);
  svr.Start();

  return 0;
}